<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="title"
    :width="width"
  >
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          :inline="true"
          label-width="80px"
          :model="tableOptions.queryForm"
        >
          <el-form-item label="表单名称">
            <el-input
              v-model="tableOptions.queryForm.formName"
              placeholder="表单名称"
            />
          </el-form-item>
          <el-form-item label="状态">
            <EasyDictSelect
              ref="statusRef"
              :default-value="tableOptions.queryForm.formStatus"
              dict-code="1002"
              show-all="Y"
              @change="handleStatusChange"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="refresh-left"
              type="warning"
              @click="handleResetQuery"
            >
              重置
            </el-button>
            <el-button icon="search" type="primary" @click="queryTableData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      :highlight-current-row="multiple"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="表单名称"
        prop="formName"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        label="状态"
        prop="formStatusDesc"
        width="70"
      >
        <template #default="{ row }">
          <el-tag :type="row.formStatus === '0' ? 'success' : 'info'">
            {{ row.formStatusDesc }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="表单设计"
        prop="formInfo"
        width="90"
      >
        <template #default="{ row }">
          <el-tag v-if="row.formInfo" type="success">已完成</el-tag>
          <el-tag v-else type="info">未完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="备注"
        prop="formRemark"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        label="绑定主表"
        prop="tableName"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        label="创建人"
        prop="createUserName"
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="datetimeFormat"
        label="创建时间"
        prop="createTime"
        width="160"
      />
      <el-table-column
        align="center"
        label="最后修改人"
        prop="updateUserName"
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="datetimeFormat"
        label="最后修改时间"
        prop="updateTime"
        width="160"
      />
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <template #footer>
      <el-button icon="circle-close" type="warning" @click="close">
        取消
      </el-button>
      <el-button icon="circle-check" type="primary" @click="save">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script setup name="ConfigFormPicker">
import { useMessage } from "@/hooks/message";
import EasyDictSelect from "@/components/dictSelect.vue";
import { queryTable } from "@/api/admin/system/configform/";
import { cloneObject, datetimeFormat } from "@/utils/common";
import { useTable } from "@/hooks/table";
import { getTableHeight } from "@/utils/util";

const props = defineProps({
  title: {
    type: String,
    default: "选择配置表单"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  },
  multiple: {
    type: Boolean,
    default: function () {
      return true;
    }
  }
});
const emits = defineEmits(["change"]);

const tableRef = ref(null);
const statusRef = ref(null);

const tableOptions = reactive({
  queryForm: {
    formTypeCode: "ALL",
    formName: "",
    formStatus: "ALL"
  },
  multiple: props.multiple,
  height: getTableHeight(3) - 60,
  queryFunction: queryTable,
  descs: ["form_name"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const state = reactive({
  dialogVisible: false
});

const handleStatusChange = val => {
  tableOptions.queryForm.formStatus = val;
  queryTableData();
};

const show = params => {
  cloneObject(tableOptions.queryForm, params);
  queryTableData();
  state.dialogVisible = true;
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

const save = () => {
  emits("change", tableOptions.dataSelections);
  close();
};

defineExpose({
  close,
  show,
  save
});
</script>
<style lang="scss">
.el-container {
  flex-direction: column;
}
</style>
